//
//  indexCollectionView.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/25.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "indexCollectionView.h"
#import "MovieModal.h"
#define Kheight  [UIScreen mainScreen].bounds.size.height
#define kwidth  [UIScreen mainScreen].bounds.size.width
@implementation indexCollectionView


- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        
        //设置代理
        self.delegate = self;
        self.dataSource = self;
        //设置分页
        self.pagingEnabled = YES;
        //注册
        [self registerClass:[indexCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        
        
    }
    
    return self;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _movieModalArray.count;
    
}


#pragma mark - set方法
- (void)setMovieModalArray:(NSArray *)movieModalArray
{
    _movieModalArray = movieModalArray;
    
    [self reloadData];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    indexCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    //传递具体某一个数据
    MovieModal *modal = _movieModalArray[indexPath.row];
    
    
    cell.modal = modal;
    
    
    return cell;
}


    


#pragma mark - 手指停止拖拽之后调用的方法，获取最终停止的位置和当前滑动的速度
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    
//    //    targetContentOffset   目标偏移量
//    
//    CGFloat ItemWidth = kwidth*3/4;
//    
//    CGFloat x = targetContentOffset->x;
//    
//    NSInteger index = x/ItemWidth;
//    
//    //赋值当前下标
//    _currentIndex = index;
//    
//    targetContentOffset->x = index * ItemWidth;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.currentIndex != indexPath.row)
   
    {
        
        [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
        _currentIndex = indexPath.row;
    }
    
    
    
}





@end
